package com.xyhuo.leetcode.december;

/**
 * 保龄球游戏的获胜者
 * https://leetcode.cn/problems/determine-the-winner-of-a-bowling-game/
 *
 * @time 2023/12/27
 */
public class IsWinner {

    public int isWinner(int[] player1, int[] player2) {
        int player1Score = calculateScore(player1);
        int player2Score = calculateScore(player2);
        int ans = 0;
        if (player1Score > player2Score) {
            ans = 1;
        } else if (player1Score < player2Score) {
            ans = 2;
        }
        return ans;
    }

    private int calculateScore(int[] player) {
        int ans = 0;
        for (int i = 0; i < player.length; i++) {
            int count = 0;
            if (i - 1 >= 0 && player[i - 1] == 10) {
                count++;
            }
            if (i - 2 >= 0 && player[i - 2] == 10) {
                count++;
            }
            ans += player[i];
            if (count > 0) {
                ans += player[i];
            }
        }
        return ans;
    }
}
